package com.cskaoyan.util.common;

import java.lang.reflect.Field;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-19 18:06
 */
public class BeanTransfer {
    //一个对象数据转到另一个对象中
    public static Object transfer(Object o1, Object o2) throws IllegalAccessException {
        Class<?> a1 = o1.getClass();
        Class<?> a2 = o2.getClass();
        Field[] fields1 = a1.getDeclaredFields();
        Field[] fields2 = a2.getDeclaredFields();
        for (Field field1 : fields1) {
            field1.setAccessible(true);
            if (field1.get(o1) != null){
                for (Field field2 : fields2) {
                    field2.setAccessible(true);
                if(field1.getName().equals(field2.getName())) {
                        field2.set(o2, field1.get(o1));
                    }
            }
            }
        }
        return o2;
    }
}
